package com.party.core.service.charge.impl;

import com.party.common.paging.Page;
import com.party.common.utils.StringUtils;
import com.party.core.dao.read.charge.PackageMemberReadDao;
import com.party.core.dao.write.charge.PackageMemberWriteDao;
import com.party.core.model.BaseModel;
import com.party.core.model.charge.PackageMember;
import com.party.core.model.charge.PackageMemberWithAuthor;
import com.party.core.model.charge.ProductType;
import com.party.core.service.charge.IPackageMemberService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * 等级实现
 *
 * @author Administrator
 */
@Service
public class PackageMemberService implements IPackageMemberService {

    @Autowired
    private PackageMemberReadDao packageMemberReadDao;
    @Autowired
    private PackageMemberWriteDao packageMemberWriteDao;

    @Override
    public String insert(PackageMember t) {
        BaseModel.preInsert(t);
        boolean result = packageMemberWriteDao.insert(t);
        if (result) {
            return t.getId();
        }
        return null;
    }

    @Override
    public boolean update(PackageMember t) {
        if (null == t)
            return false;
        t.setUpdateDate(new Date());
        return packageMemberWriteDao.update(t);
    }

    @Override
    public boolean deleteLogic(String id) {
        if (StringUtils.isBlank(id))
            return false;

        return packageMemberWriteDao.deleteLogic(id);
    }

    @Override
    public boolean delete(String id) {
        if (StringUtils.isBlank(id))
            return false;

        return packageMemberWriteDao.delete(id);
    }

    @Override
    public boolean batchDeleteLogic(Set<String> ids) {
        if (CollectionUtils.isEmpty(ids))
            return false;

        return packageMemberWriteDao.batchDeleteLogic(ids);
    }

    @Override
    public boolean batchDelete(Set<String> ids) {
        if (CollectionUtils.isEmpty(ids))
            return false;

        return packageMemberWriteDao.batchDelete(ids);
    }

    @Override
    public PackageMember get(String id) {
        return packageMemberReadDao.get(id);
    }

    @Override
    public List<PackageMember> listPage(PackageMember t, Page page) {
        return packageMemberReadDao.listPage(t, page);
    }

    @Override
    public List<PackageMember> list(PackageMember t) {
        return packageMemberReadDao.listPage(t, null);
    }

    @Override
    public List<PackageMember> batchList(Set<String> ids, PackageMember t, Page page) {
        if (CollectionUtils.isEmpty(ids))
            return Collections.EMPTY_LIST;

        return packageMemberReadDao.batchList(ids, new HashedMap(), page);
    }

    @Override
    public List<PackageMember> newListPage(PackageMember packageMember, Map<String, Object> params) {
        return packageMemberReadDao.newListPage(packageMember, params);
    }

    @Override
    public PackageMember getMaxPackageRole(PackageMember packageMember, Map<String, Object> params) {
        return packageMemberReadDao.getMaxPackageRole(packageMember, params);
    }

    @Override
    public List<PackageMember> getPackageMemberByType(String memberId, int type, Page page) {
        return packageMemberReadDao.getPackageMemberByType(memberId, type, page);
    }

    @Override
    public PackageMember findByOrderId(String orderId) {
        return packageMemberReadDao.findByOrderId(orderId);
    }


    /**
     * @Description:系统购买列表
     * @Param:
     * @return:
     * @Author: Hua Qunhao
     * @Date: 2019/5/23
     */
    @Override
    public List<PackageMember> systemBuyList(PackageMember packageMember, Page page) {
        return packageMemberReadDao.systemBuyList(packageMember, page);
    }

    @Override
    public boolean isUnpaidOrder(String memberId) {
        List<PackageMember> packageMembers = queryAllUnpaidOrder(memberId);
        return (packageMembers != null && packageMembers.size() > 0);
    }

    /**
     * @Description:查询所有有待付款和审核中的订单
     * @Param:memberId
     * @return:
     * @Author: Hua Qunhao
     * @Date: 2019-05-23-0023
     */
    @Override
    public List<PackageMember> queryAllUnpaidOrder(String memberId) {
        Map<String, Object> hashedMap = new HashedMap();
        hashedMap.put("type", ProductType.SYSTEM_TYPE.getCode());
        hashedMap.put("sign", true);
        PackageMember queryPackageMember = new PackageMember();
        queryPackageMember.setMemberId(memberId);
        List<PackageMember> packageMembers = packageMemberReadDao.newListPage(queryPackageMember, hashedMap);
        return packageMembers;
    }

    @Override
    public List<PackageMemberWithAuthor> packageMemberWebList(PackageMember packageMember, Map<String, Object> params, Page page) {
        return packageMemberReadDao.packageMemberWebList(packageMember, params, page);
    }

    @Override
    public List<PackageMember> queryUnpaidPayPMList(String createDate) {
        Map<String, Object> hashedMap = new HashedMap();
        hashedMap.put("type", ProductType.SYSTEM_TYPE.getCode());
        hashedMap.put("sign", true);
        hashedMap.put("createDate", createDate);
        List<PackageMember> packageMembers = packageMemberReadDao.newListPage(new PackageMember(), hashedMap);
        return packageMembers;
    }

    /**
     * @Author hzq
     * @Description //根据memberId获取最近支付成功的packageMember
     * @Date 13:44 2019/5/30
     * @param memberId 机构id
     * @return
     **/
    @Override
    public List<PackageMember> getLatelyPaySuccess(String memberId) {
        return packageMemberReadDao.getLatelyPaySuccess(memberId);
    }
}
